using System;
using System.Collections.Generic;
using System.Text;

namespace NBusiness.Data
{
    [Serializable]
    public abstract class ValidationRule
    {
        private ValidationRuleHandler _rule;
        private ValidationEventArgs _arg;

        private Type _entityType;
        /// <summary>
        /// The type of entity this rule applies to.
        /// </summary>
        public Type EntityType
        {
            get { return _entityType; }
            set { _entityType = value; }
        }

        private string _field;
        /// <summary>
        /// The field this rule applies to.
        /// </summary>
        public string Field
        {
            get { return _field; }
        }

        private string _errorMessage;
        /// <summary>
        /// The friendly text for this particular validation rule when it is broken.
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
        }

        public abstract ValidationRuleType Type { get; }

        protected ValidationRule(Type entityType, string field, ValidationRuleHandler rule, ValidationEventArgs arg, string errorMessage)
        {
            _entityType = entityType;
            _field = field;
            _rule = rule;
            _arg = arg;
            _errorMessage = errorMessage;
        }

        public bool Validate(object value)
        {
            return _rule(value, _arg);
        }
    }
}
